package com.xhwl.centiotdata.pollingtrigger.integration;


import com.xhwl.centiotdata.handler.phonogram.PhonogramIntegrationFactory;
import com.xhwl.centiotdata.handler.phonogram.PhonogramIntegrationHandler;
import com.xhwl.centiotdata.handler.weigh.WeighIntegrationFactory;
import com.xhwl.centiotdata.handler.weigh.WeighIntegrationHandler;
import com.xhwl.centiotdata.integration.parkingguidance.ParkingGuidanceFactory;
import com.xhwl.centiotdata.integration.parkingguidance.ParkingGuidanceIntegrationConfig;
import com.xhwl.centiotdata.integration.parkingguidance.ParkingGuidanceSystemHandler;
import com.xhwl.centiotdata.pojo.dto.brodcast.PhonogramConfigDTO;
import com.xhwl.centiotdata.pojo.dto.weigh.WeighConfigDTO;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.integrateconfig.PhonogramIntegrationConfig;
import com.xhwl.centiotdata.service.integrateconfig.WeighIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class vehicleScheduleDispatcher implements ScheduleDispatcher {

    @Autowired
    private PhonogramIntegrationConfig phonogramIntegrationConfig;

    @Autowired
    private WeighIntegrationConfig weighIntegrationConfig;

    @Autowired
    private ParkingGuidanceFactory parkingGuidanceFactory;

    @Autowired
    private IDeviceCentService deviceCentService;

    @Override
    public void doTenMinutesTask() {
        //拉取音柱和地磅设备信息
        ThreadPoolUtils.executor.execute(this::executePullDevice);
    }

    public void executePullDevice(){
        Map<Integer, List<PhonogramConfigDTO>> integrationConfigMap = phonogramIntegrationConfig.getIntegrationConfig();

        integrationConfigMap.forEach(
                (key, value) -> {
                    if (!value.isEmpty()) {
                        value.forEach(integrationConfig -> {
                            try {
                                PhonogramIntegrationHandler invokeStrategy = PhonogramIntegrationFactory.getInvokeStrategy(integrationConfig.getProductId());
                                invokeStrategy.pullYinZhuDevice(integrationConfig);
                            } catch (Exception e) {
                                log.error("项目id为{}，拉取音柱设备出现问题，问题为{}", integrationConfig.getProjectId(), e.getMessage());
                            }
                        });
                    }
                }
        );

        Map<Integer, List<WeighConfigDTO>> integrationConfigMap2 = weighIntegrationConfig.getIntegrationConfig();
        integrationConfigMap2.forEach(
                (key, value) -> {
                    if (!value.isEmpty()) {
                        value.forEach(integrationConfig -> {
                            try {
                                WeighIntegrationHandler invokeStrategy = WeighIntegrationFactory.getInvokeStrategy(integrationConfig.getProductId());
                                invokeStrategy.pullWeighDevice(integrationConfig);
                            } catch (Exception e) {
                                log.error("项目id为{}，拉取音柱设备出现问题，问题为{}", integrationConfig.getProjectId(), e.getMessage());
                            }
                        });
                    }
                }
        );

        Map<Integer, List<ParkingGuidanceIntegrationConfig>> integerListMap = parkingGuidanceFactory.onFindingIntegrationConfigs();
        integerListMap.forEach(
                (key, value)->{
                    if(!value.isEmpty()){
                        value.forEach(parkingGuidanceIntegrationConfig -> {
                            try {
                                ParkingGuidanceSystemHandler parkingGuidanceSystemHandler = parkingGuidanceFactory.onFindingIntegrationHandler(parkingGuidanceIntegrationConfig.getProductName());
                                List<DeviceDTO> deviceDTOS = parkingGuidanceSystemHandler.onGetSpaceForDevice(parkingGuidanceIntegrationConfig);
                                deviceCentService.asyncUpdateDevice(deviceDTOS);
                            } catch (Exception e) {
                                log.error("项目id为{}，拉取地磁设备出现问题，问题为{}",parkingGuidanceIntegrationConfig.getProjectId(), e.getMessage());
                            }
                        });
                    }
                }
        );
    }
}
